package com.example.dormmanage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormmanage.dao.ILossDao;
import com.example.dormmanage.dao.IRoomDao;
import com.example.dormmanage.dao.IStudentDao;
import com.example.dormmanage.entity.Loss;
import com.example.dormmanage.entity.Move;
import com.example.dormmanage.entity.Room;
import com.example.dormmanage.entity.Student;
import com.example.dormmanage.service.ILossService;
import com.example.dormmanage.util.BaseContext;
import com.example.dormmanage.view.LossDto;
import com.example.dormmanage.view.MoveDto;
import com.example.dormmanage.view.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LossService extends ServiceImpl<ILossDao, Loss> implements ILossService {

    @Autowired
    private IStudentDao studentDao;

    @Autowired
    private IRoomDao roomDao;

    //管理员查询钥匙丢失列表
    public ResultView<Page> selectLossList(Integer page, Integer pageSize, Integer buildingId, String studentName) {
        Page<LossDto> pageInfo = new Page<>(page, pageSize);
        //根据楼宇号查询楼宇丢失钥匙的学生
        LambdaQueryWrapper<Loss> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Loss::getBuildingId, buildingId);
        List<Loss> lossList = this.list(lqw);
        List<LossDto> lossDtoList = lossList.stream().map(item -> {
            LossDto lossDto = new LossDto();
            //查询学生用户名称
            LambdaQueryWrapper<Student> stuLqw = new LambdaQueryWrapper<>();
            stuLqw.eq(Student::getStudentId, item.getStudentId())
                    .like(studentName != null, Student::getUserName, studentName);
            Student student = studentDao.selectOne(stuLqw);
            //封装数据
            lossDto.setStudentName(student.getRealName());
            String status = null;
            //是否归还
            if (item.getStatus() == 1) {
                status = "未归还";
            } else {
                status = "已归还";
            }
            //审核状态
            String checkStatus = null;
            if (item.getCheckStatus() == 1) {
                checkStatus = "通过";
            } else {
                checkStatus = "不通过";
            }
            BeanUtil.copyProperties(item, lossDto);
            lossDto.setStatus(status);
            lossDto.setCheckStatus(checkStatus);
            lossDto.setUserName(student.getUserName());
            return lossDto;
        }).collect(Collectors.toList());
        pageInfo.setRecords(lossDtoList);
        return ResultView.success(pageInfo);
    }

    //学生查询钥匙丢失列表
    public ResultView<Page> selectLossList(Integer page, Integer pageSize) {
        Page<LossDto> pageInfo = new Page<>(page, pageSize);
        //获取学生id
        Long studentId = BaseContext.getCurrentId();
        //根据学生id查询loss表
        LambdaQueryWrapper<Loss> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Loss::getStudentId, studentId);
        List<Loss> lossList = this.list(lqw);
        List<LossDto> lossDtoList = lossList.stream().map(item -> {
            LossDto lossDto = new LossDto();
            Student student = studentDao.selectById(studentId);
            //封装数据
            lossDto.setStudentName(student.getRealName());
            String status = null;
            //是否归还
            if (item.getStatus() == 1) {
                status = "未归还";
            } else {
                status = "已归还";
            }
            //审核状态
            String checkStatus = null;
            if (item.getCheckStatus() == 0) {
                checkStatus = "不通过";
            } else if(item.getCheckStatus() == 1) {
                checkStatus = "通过";
            }else{
                checkStatus = "待审核";
            }
            BeanUtil.copyProperties(item, lossDto);
            lossDto.setStatus(status);
            lossDto.setCheckStatus(checkStatus);
            lossDto.setUserName(student.getUserName());
            return lossDto;
        }).collect(Collectors.toList());
        pageInfo.setRecords(lossDtoList);
        return ResultView.success(pageInfo);
    }

    //管理员审核
    public ResultView<String> checkLoss(Long lossId, Integer checkStatus) {
        LambdaUpdateWrapper<Loss> luw = new LambdaUpdateWrapper<>();
        luw.eq(Loss::getLossId,lossId)
                .set(Loss::getCheckStatus,checkStatus);
        this.update(luw);
        return ResultView.success("审核成功");
    }

    //学生新增钥匙丢失记录
    public ResultView<String> addLoss(Loss loss) {
        Long studentId = BaseContext.getCurrentId();
        loss.setStudentId(studentId);
        loss.setUploadTime(LocalDateTime.now());
        //查询buildingId
        Student student = studentDao.selectById(studentId);
        Room room = roomDao.selectById(student.getRoomId());
        loss.setBuildingId(room.getBuildingId());
        this.save(loss);
        return ResultView.success("提交成功");
    }
}
